/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.dc.task;

import com.cxhd.dc.DataConfig;
import com.cxhd.dc.tool.FileUtil;
import com.google.common.io.Files;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class ScanFolderToSendPacsQueueTask extends BaseTask {

    private BlockingQueue sendPacsQueue;
    private File[] fileDicomFolder;

    public ScanFolderToSendPacsQueueTask(BlockingQueue sendPacsQueue) {
        this.sendPacsQueue = sendPacsQueue;
    }

    @Override
    public BaseTask start() {
        fileDicomFolder = new File(DataConfig.getInstance().getBasePath() + "/" + DataConfig.getInstance().getDicomSendFolder()).listFiles();

        return this;
    }

    @Override
    public BaseTask doTask() {

        for (File file : fileDicomFolder) {
            if (file.isDirectory() && file.list().length > 0) {
                makeSendFolder(file);
            }
        }
        return this;
    }

    @Override
    public BaseTask stop() {
        return this;
    }

    @Override
    public BaseTask cancelTask() {
        return this;
    }

    public void makeSendFolder(File f) {
        String device = f.getName();
        try {
            File moveFile = null;
            if (checkFileDelete(device)) {
                moveFile = new File(DataConfig.getInstance().getBasePath() + "/" + DataConfig.getInstance().getMoveFileFolder() + device + "/");
                moveFile.mkdirs();
            }
            File sendFile = new File(f.getAbsolutePath() + "/" + new Date().getTime() + "/");
            sendFile.mkdirs();
            boolean sendFlag = false;
            for (File s : f.listFiles()) {
                if (s.isFile()) {
                    // System.out.println(s.getAbsolutePath()+"-------------"+sendFile.getAbsolutePath() + "/" + s.getName());
                    //修改文件的sopiuid
                    sendFlag = true;
                    if (moveFile != null) {
                        Files.copy(s, new File(moveFile.getAbsolutePath() + "/" + s.getName()));
                    } else {
                        //System.out.println("修改SOPIIUID");
                        //editSopIuid(s, device, f.getAbsolutePath());
                    }

                    Files.move(s, new File(sendFile.getAbsolutePath() + "/" + s.getName()));

                }
            }
            if (sendFlag) {
                sendPacsQueue.put(sendFile.getAbsolutePath() + "@" + device);
            } else {
                sendFile.delete();
            }
        } catch (Exception e) {
        }
    }

    private void editSopIuid(File s, String device, String path) {
        try {
            String lastChar = device.substring(device.length() - 4, device.length());
            String sopiuid = FileUtil.getSopiuid(s.getAbsolutePath()) + lastChar;
            System.out.println("new sopiuid="+sopiuid);
            String filePath = path + "/" + sopiuid;
            Runtime r = Runtime.getRuntime();

            Process p = r.exec("c:/cxhd2015/tools/dcmodify.exe -nb +fo -m 0008,0018=" + sopiuid + " " + s.getAbsolutePath());

            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String msg = null;
            while ((msg = br.readLine()) != null) {

            }
            BufferedReader er = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            String error = null;
            while ((error = er.readLine()) != null) {

            }
            //p.waitFor();
            p.destroy();
        } catch (Exception e) {
        }

    }

    public boolean checkFileDelete(String device) {
        String sns = DataConfig.getInstance().getMoveFile_sn();
        if (sns != null) {
            String sn[] = sns.split(",");
            for (String s : sn) {
                if (s.equals(device)) {
                    return true;
                }
            }
        }
        return false;
    }

}
